<template>
  <el-dialog
    title="高级设置"
    :visible.sync="advancedShow"
    :destroy-on-close="true"
    :append-to-body="true"
    width="45%"
    @close="handleClose"
    class="edit-main"
  >
    <el-form
      ref="formData"
      :model="formData"
      :rules="rules"
      label-width="150px"
    >
      <el-form-item label="排队语音：" prop="lineVoice">
        <el-radio v-model="formData.lineVoice" label="1">默认</el-radio>
        <el-radio v-model="formData.lineVoice" label="2">静音</el-radio>
      </el-form-item>
      <el-form-item label="提示录音：" prop="promptTape">
        <el-radio v-model="formData.promptTape" label="1">是</el-radio>
        <el-radio v-model="formData.promptTape" label="2">否</el-radio>
      </el-form-item>
      <el-form-item label="播报排队数：" prop="lineNumber">
        <el-radio v-model="formData.lineNumber" label="1">是</el-radio>
        <el-radio v-model="formData.lineNumber" label="2">否</el-radio>
      </el-form-item>
      <el-form-item label="播报工号：" prop="workNumber">
        <el-radio v-model="formData.workNumber" label="1">是</el-radio>
        <el-radio v-model="formData.workNumber" label="2">否</el-radio>
      </el-form-item>
      <el-form-item label="号码状态检测：" prop="numberStateTest">
        <el-radio v-model="formData.numberStateTest" label="1">是</el-radio>
        <el-radio v-model="formData.numberStateTest" label="2">否</el-radio>
      </el-form-item>
      <el-form-item label="排行榜：:" prop="ranking">
        <el-radio v-model="formData.ranking" label="1">是</el-radio>
        <el-radio v-model="formData.ranking" label="2">否</el-radio>
      </el-form-item>
      <el-form-item label="呼叫超时（秒）：" prop="callTimeOut">
        <el-input
          type="input"
          placeholder="请输入内容"
          v-model="formData.callTimeOut"
          maxlength="200"
          show-word-limit
        >
        </el-input>
        <div>触发时长（-1不发送，0全部发送，>0 多少秒发送）</div>
      </el-form-item>
      <el-form-item label="解密URL：" prop="decryptUrl">
        <el-input
          type="input"
          placeholder="请输入内容"
          v-model="formData.decryptUrl"
          maxlength="200"
          show-word-limit
        >
        </el-input>
      </el-form-item>
      <el-form-item label="触发时长：" prop="tiggerDuration">
        <el-input
          type="input"
          placeholder="请输入内容"
          v-model="formData.tiggerDuration"
          maxlength="200"
          show-word-limit
        >
        </el-input>
      </el-form-item>
      <el-form-item label="短信内容：" prop="message">
        <el-input
          type="textarea"
          placeholder="请输入内容"
          v-model="formData.message"
          maxlength="200"
          show-word-limit
        >
        </el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="onSubmit">提 交</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  getProjectDetail,
  updateProject,
} from "@/api/projectManagement/list.js";
import { Message } from "element-ui";
export default {
  name: "advancedSettingDialog",
  props: {
    advancedSettingDialogShow: {
      type: Boolean,
      default: false,
    },
    id: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      formData: {
        lineVoice: "",
        promptTape: "",
        lineNumber: "",
        workNumber: "",
        numberStateTest: "",
        ranking: "",
        callTimeOut: "",
        decryptUrl: "",
        tiggerDuration: "",
        message: "",
      },
      rules: {
        questionText: [
          { required: true, message: "内容不能为空", trigger: "blur" },
        ],
      },
      total: 0,
      advancedShow: false,
    };
  },

  watch: {
    advancedSettingDialogShow: {
      handler(val) {
        console.log(val, "val");

        this.advancedShow = val;
      },
      immediate: true,
    },
    id: {
      handler(val) {
        if (val) {
          this.getProjectDetail();
        }
      },
    },
  },
  methods: {
    // 获取项目详情
    async getProjectDetail() {
      const { code, data } = await getProjectDetail(this.id);
      if (code === 200) {
        this.getFormData(data);
      }
    },

    getFormData(data) {
      for (const key in this.formData) {
        if (this.formData.hasOwnProperty(key) && data.hasOwnProperty(key)) {
          this.formData[key] = data[key] == 0 || data[key] ? data[key] : "";
        }
      }
    },

    // 保存
    async onSubmit() {
      const { callTimeOut } = this.formData;
      const params = {
        id: this.id,
        ...this.formData,
        callTimeOut: callTimeOut == 0 || callTimeOut ? Number(callTimeOut) : "",
      };

      const { msg, code } = await updateProject(params);
      if (code === 200) {
        Message.success(msg);
        this.handleClose()
      } else {
        Message.error(msg);
      }
    },

    handleClose() {
      this.formData= {
        lineVoice: "",
        promptTape: "",
        lineNumber: "",
        workNumber: "",
        numberStateTest: "",
        ranking: "",
        callTimeOut: "",
        decryptUrl: "",
        tiggerDuration: "",
        message: "",
      }
      this.$emit("onAdvancedDialog");
    },
  },
};
</script>

<style lang="scss" scoped>
.edit-main {
  ::v-deep .el-textarea__inner {
    height: 150px;
  }
}
</style>
